#ifndef __PROXY_H__
#define __PROXY_H__

#include "Connection.h"
#include "SSLConnection.h"

class Proxy
{
public:
	// ctor
	Proxy();
	~Proxy();
	
	// set normal server
	int SetNormalServer( const unsigned short listen_port );
	
	// set ssl server
	int SetSSLServer( const unsigned short ssl_listen_port, const char* ca_crt_filename, const char* srv_crt_filename, const char* srv_key_filename );
	
	// start server
	int Start();
	
	// stop
	int Stop();

private:
	// exit flag
	bool m_is_exit;

	///////////////////////////////////////////////////////////////////////////////////////
	// for normal server
	///////////////////////////////////////////////////////////////////////////////////////
	// server listen socket
	int m_listen_socket;
	// server listen port (default to 60000)
	unsigned short m_listen_port;
	///////////////////////////////////////////////////////////////////////////////////////
	
	///////////////////////////////////////////////////////////////////////////////////////
	// for ssl server
	///////////////////////////////////////////////////////////////////////////////////////
	entropy_context m_ssl_entropy;
	ctr_drbg_context m_ssl_ctr_drbg;
	x509_cert m_ssl_ca_certificate;
	x509_cert m_ssl_server_certificate;
	rsa_context m_ssl_server_privatekey;
	// server listen socket
	int m_ssl_listen_socket;
	// server listen port (default to 60001)
	unsigned short m_ssl_listen_port;
	///////////////////////////////////////////////////////////////////////////////////////
	
	// listen thread
	pthread_t m_hListenPThread;
	static void* ListenThread( void* p_arg );

	// process connection thread
	pthread_t m_hProcessPThread;
	static void* ProcessThread( void* p_arg );
	
	// connection list
	pthread_mutex_t m_connection_list_pthread_mutex;
	std::vector<Connection*> m_connection_list;
	
	// start server
	int StartNormalServer();
	int StartSSLServer();
	
	// accept connection
	int AcceptNormalConnection();
	int AcceptSSLConnection();
	int SetReadFds( fd_set* p_read_fds, int* p_max_read_fd );
};

#endif /* __PROXY_H__ */
